﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ImageProcessing.Forms
{
    public partial class puzzleGameForm : Form
    {
        DImage oldImage=new DImage();
        Bitmap bigimage;
        Bitmap[,] parts = new Bitmap[3, 4];
        bool mousedown=false;
        int firstPointX, firstPointY;
        bool mousenter = false;
        PictureBox currentMove;
        bool donemove = false;
        bool[,] isFill = new bool[3,4];
        string[,] currentImageInNewpcb = new string[3, 4];
        PictureBox[,] oldParts = new PictureBox[3, 4];
        PictureBox[,] newParts = new PictureBox[3, 4];

        Point[,] startLocation = new Point[3, 4];

        public puzzleGameForm()
        {
            InitializeComponent();
            if (Main.ImageList.Count == 0)
                return;

            #region startLocation
            startLocation[0, 0] = PcbPart11.Location;
            startLocation[0, 1] = PcbPart12.Location;
            startLocation[0, 2] = PcbPart13.Location;
            startLocation[0, 3] = PcbPart14.Location;
            startLocation[1, 0] = PcbPart21.Location;
            startLocation[1, 1] = PcbPart22.Location;
            startLocation[1, 2] = PcbPart23.Location;
            startLocation[1, 3] = PcbPart24.Location;
            startLocation[2, 0] = PcbPart31.Location;
            startLocation[2, 1] = PcbPart32.Location;
            startLocation[2, 2] = PcbPart33.Location;
            startLocation[2, 3] = PcbPart34.Location;
            #endregion

            #region setnewPArts
            newParts[0, 0] = Pbcnew11;
            newParts[0, 1] = Pbcnew12;
            newParts[0, 2] = Pbcnew13;
            newParts[0, 3] = Pbcnew14;
            newParts[1, 0] = Pbcnew21;
            newParts[1, 1] = Pbcnew22;
            newParts[1, 2] = Pbcnew23;
            newParts[1, 3] = Pbcnew24;
            newParts[2, 0] = Pbcnew31;
            newParts[2, 1] = Pbcnew32;
            newParts[2, 2] = Pbcnew33;
            newParts[2, 3] = Pbcnew34;
            #endregion
            foreach (DImage item in Main.ImageList)
            {
                 this.CmbImage.Items.Add(item.Name);
            }

            this.CmbImage.SelectedIndex = 0;
            oldImage.ImageStructArray = Main.ImageList[0].ImageStructArray;
            PcbOldImage.Image = Main.ImageList[0].ImageBitmap;
            bigimage = (Bitmap)this.PictureBoxZoom(Main.ImageList[0].ImageBitmap, 600, 450);


        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the CmbImage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CmbImage_SelectedIndexChanged(object sender, EventArgs e)
        {
            oldImage.ImageStructArray = Main.ImageList[CmbImage.SelectedIndex].ImageStructArray;
            PcbOldImage.Image = Main.ImageList[CmbImage.SelectedIndex].ImageBitmap;
            bigimage = (Bitmap)this.PictureBoxZoom(Main.ImageList[CmbImage.SelectedIndex].ImageBitmap, 600, 450);
        }

        private void label2_Click(object sender, EventArgs e)
        {
            SetParts();
            #region set pcb
            oldParts[0,0] = PcbPart11;
            oldParts[0, 1] = PcbPart12;
            oldParts[0, 2] = PcbPart13;
            oldParts[0, 3] = PcbPart14;
            oldParts[1, 0] = PcbPart21;
            oldParts[1, 1] = PcbPart22;
            oldParts[1, 2] = PcbPart23;
            oldParts[1, 3] = PcbPart24;
            oldParts[2, 0] = PcbPart31;
            oldParts[2, 1] = PcbPart32;
            oldParts[2, 2] = PcbPart33;
            oldParts[2, 3] = PcbPart34;

            #endregion
           
            #region startLocation
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    oldParts[i, j].Location = startLocation[i, j];
                    oldParts[i, j].Visible = true;
                }
                
            }
            #endregion
            pcbResult.Image = null;

            #region resetpcb new

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    newParts[i, j].Image = null;
                    newParts[i, j].BringToFront();
                }

            }
            #endregion


        }

        private Bitmap Copy(Bitmap srcBitmap, Rectangle section)
        {
            // Create the new bitmap and associated graphics object
            Bitmap bmp = new Bitmap(section.Width, section.Height);
            Graphics g = Graphics.FromImage(bmp);

            // Draw the specified section of the source bitmap to the new one
            g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

            // Clean up
            g.Dispose();

            // Return the bitmap
            return bmp;
        }

        private void SetParts()
        {
            int x=0,y=0;
           System.Drawing.Rectangle temp;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    temp=new Rectangle(x,y,150,150);
                    parts[i, j] = this.Copy(bigimage, temp);
                        x += 150;
                }
                y += 150;
                x = 0;

            }
            #region set pcb
            PcbPart11.Image = parts[0, 0];
            PcbPart12.Image = parts[0, 1];
            PcbPart13.Image = parts[0, 2];
            PcbPart14.Image = parts[0, 3];
            PcbPart21.Image = parts[1, 0];
            PcbPart22.Image = parts[1, 1];
            PcbPart23.Image = parts[1, 2];
            PcbPart24.Image = parts[1, 3];
            PcbPart31.Image = parts[2, 0];
            PcbPart32.Image = parts[2, 1];
            PcbPart33.Image = parts[2, 2];
            PcbPart34.Image = parts[2, 3];

            #endregion

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    isFill[i, j] = false;
                }

            }
        }

        public Image PictureBoxZoom(Image img, int width,int height)
        {
            Bitmap bm = new Bitmap(img, width,height);
            Graphics grap = Graphics.FromImage(bm);
            grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
            return bm;
        }
        private void label2_MouseEnter(object sender, EventArgs e)
        {
            label2.ForeColor = Color.DeepPink;
        }

        private void label2_MouseLeave(object sender, EventArgs e)
        {
            label2.ForeColor = Color.White;
        }

        private void PcbPart11_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox p = (PictureBox)sender;
            this.currentMove = (PictureBox)sender;
            string pcbNum = p.Name.Substring(7, 2);
            this.mousedown = true;
            this.firstPointX = e.X;
            this.firstPointY = e.Y;
           // this.Invalidate();
        }

        private void PcbPart11_MouseMove(object sender, MouseEventArgs e)
        {
            if (!mousedown)
            {
                return;
            }

           // PcbPart11.Location = e.Location;
            PictureBox pcb = (PictureBox)sender;
            this.currentMove = (PictureBox)sender;


            Point p=new Point();
            p = pcb.Location;
           // int deltaX 
            p.X = e.X - this.firstPointX + pcb.Location.X;
           // int deltaY
            p.Y = e.Y - this.firstPointY + pcb.Location.Y;
            pcb.Location = p;
            //PcbPart11.Location = deltaY;
            this.firstPointX = e.X;
            this.firstPointY = e.Y;
           //this.Invalidate();

        }

        private void PcbPart11_MouseUp(object sender, MouseEventArgs e)
        {
            this.mousedown = false;
            this.donemove = true;
           // this.Invalidate();

        }

        /// <summary>
        /// Handles the MouseEnter event of the Pbcnew11 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Pbcnew11_MouseEnter(object sender, EventArgs e)
        {
            PictureBox p = (PictureBox)sender;
          
            int  xnewpcb = Convert.ToInt32( p.Name.Substring(6,1));
            int ynewpcb = Convert.ToInt32(p.Name.Substring(7, 1));
            xnewpcb--;
            ynewpcb--;
            mousenter = true;
           
            if (donemove)
            {
                if(!isFill[xnewpcb, ynewpcb])
                {
                int x = Convert.ToInt32( currentMove.Name.Substring(7, 1));
                int y = Convert.ToInt32(currentMove.Name.Substring(8, 1));
                p.Image = this.currentMove.Image;
                isFill[xnewpcb, ynewpcb] = true;
                x--;
                y--;
                currentImageInNewpcb[xnewpcb , ynewpcb] = x.ToString() + y.ToString();
                this.currentMove.Visible = false;
                }
                donemove = false;
            }


        }

        /// <summary>
        /// Handles the MouseDown event of the Pbcnew11 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void Pbcnew11_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox p = (PictureBox)sender;

            
            int xnewpcb = Convert.ToInt32(p.Name.Substring(6, 1));
            int ynewpcb = Convert.ToInt32(p.Name.Substring(7, 1));
            if (isFill[xnewpcb-1, ynewpcb-1])
            {
                int x = currentImageInNewpcb[xnewpcb - 1, ynewpcb - 1][0] - 48;
                int y = currentImageInNewpcb[xnewpcb - 1, ynewpcb - 1][1] - 48;

                oldParts[x, y].Visible = true;
                oldParts[x, y].Location = p.Location;
                p.Image = null;
                isFill[xnewpcb - 1, ynewpcb - 1] = false;
            }
        }

        /// <summary>
        /// Handles the MouseEnter event of the label3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void label3_MouseEnter(object sender, EventArgs e)
        {
            label3.ForeColor = Color.DeepPink;
        }

        /// <summary>
        /// Handles the MouseLeave event of the label3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void label3_MouseLeave(object sender, EventArgs e)
        {
            label3.ForeColor = Color.White;
        }

        /// <summary>
        /// Handles the Click event of the label3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void label3_Click(object sender, EventArgs e)
        {
            bool trueGame = true;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (currentImageInNewpcb[i, j] == null)
                    {
                        trueGame = false;
                        continue;
                    }

                    if (currentImageInNewpcb[i, j][0]-48 != i || currentImageInNewpcb[i, j][1]-48 != j)
                    {
                        trueGame = false;
                        break;
                    }
                }
            }

            if (trueGame)
            {
                pcbResult.Image = bigimage;
                pcbResult.BringToFront();
                MessageBox.Show("you win ");
                return;
            }

            MessageBox.Show("try again ");
            return;
                
        }
    }
}
